package com.dure.hotmemory.scenes;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dure.commonlib.util.ImageUtil;
import com.dure.commonlib.util.PreferencesUtils;
import com.dure.commonlib.util.ToastUtils;
import com.dure.commonlib.widgets.CircleImageView;
import com.dure.hotmemory.R;
import com.dure.hotmemory.bean.Bean;
import com.dure.hotmemory.controllers.HomeController;
import com.dure.hotmemory.drawview.BaseSurfaceView;
import com.dure.hotmemory.listeners.ConfirmListener;
import com.dure.hotmemory.utils.DialogUtil;
import com.dure.hotmemory.utils.ShareUtil;

/**
 * Created by leix on 12/12/2016.
 */

public class HomeScene extends BaseScene {
    FrameLayout backFL;
    RelativeLayout aboutAppLayout, appsRL, adView, shareRL, scoreRL,rewardRL;

    LinearLayout login;
    ImageView arrowIV;
    TextView loginTV,logoutTV;
    CircleImageView headIV;
    View appRedView;
    private HomeController homeController;
    private ImageView settingIV, vipIV;

    public HomeScene(BaseSurfaceView baseSurfaceView) {
        super(baseSurfaceView);
        homeController = new HomeController(this);
        initData();
    }

    @Override
    public void initData() {
        sceneView = LayoutInflater.from(activity).inflate(R.layout.home_activity, null);
        findViews();
        initView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            backFL.setBackgroundResource(R.drawable.white_planet_drawable);
            vipIV.setBackgroundResource(R.drawable.white_planet_drawable);
            logoutTV.setBackgroundResource(R.drawable.white_planet_drawable);
        }else{
            backFL.setBackgroundResource(R.drawable.white_circle_drawable);
            vipIV.setBackgroundResource(R.drawable.white_circle_drawable);
            logoutTV.setBackgroundResource(R.drawable.white_circle_drawable);
        }
    }

    private void findViews() {
        backFL = (FrameLayout) sceneView.findViewById(R.id.back_fl);

        aboutAppLayout = (RelativeLayout) sceneView.findViewById(R.id.about_layout);
        appsRL = (RelativeLayout) sceneView.findViewById(R.id.app_layout);

        login = (LinearLayout) sceneView.findViewById(R.id.login_ll);
        adView = (RelativeLayout) sceneView.findViewById(R.id.bottom_ad_layout);
        headIV = (CircleImageView) sceneView.findViewById(R.id.head_iv);
        arrowIV = (ImageView) sceneView.findViewById(R.id.arrow_iv);
        logoutTV = (TextView) sceneView.findViewById(R.id.logout_tv);
        vipIV = (ImageView) sceneView.findViewById(R.id.vip_iv);
        loginTV = (TextView) sceneView.findViewById(R.id.login_tv);
        rewardRL = (RelativeLayout) sceneView.findViewById(R.id.tip_layout);

        shareRL = (RelativeLayout) sceneView.findViewById(R.id.share_layout);
        scoreRL = (RelativeLayout) sceneView.findViewById(R.id.score_layout);
        login = (LinearLayout) sceneView.findViewById(R.id.login_ll);
        appRedView = sceneView.findViewById(R.id.app_red_view);

        addListener();
    }

    private void addListener() {
        backFL.setOnClickListener(activity);
        aboutAppLayout.setOnClickListener(activity);
        appsRL.setOnClickListener(activity);

        login.setOnClickListener(activity);

        scoreRL.setOnClickListener(activity);
        logoutTV.setOnClickListener(activity);
        vipIV.setOnClickListener(activity);
        rewardRL.setOnClickListener(activity);

        shareRL.setOnClickListener(activity);
        login.setOnClickListener(activity);

    }

    private void initView() {
        if (!PreferencesUtils.getBoolean(context, "apps_entered")) {//显示小红点
            appRedView.setVisibility(View.VISIBLE);
        } else {
            appRedView.setVisibility(View.GONE);
        }


        changeLoginState();
    }

    @Override
    public void permissionGranted(int requestCode) {
        super.permissionGranted(requestCode);
        ShareUtil.showCustomShareDialog(activity);
    }

    @Override
    public void viewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_fl:
                activity.onBackPressed();
                break;
            case R.id.about_layout:
                activity.moveToNewScene(new AboutAppScene(baseSurfaceView));
                break;
            case R.id.app_layout:
                activity.moveToNewScene(new AppScene(baseSurfaceView));
                PreferencesUtils.putBoolean(context, "apps_entered", true);
                appRedView.setVisibility(View.GONE);
                break;
            case R.id.score_layout:
                DialogUtil.showShareDialog(activity, "感谢您的支持", "感谢您对热记忆的支持, 如果您觉得热记忆还不错, 可以给我们打打分， 帮助我们把软件做得更好, 谢谢!");
                break;


            case R.id.share_layout:
                homeController.checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE, 1, new ConfirmListener() {

                    @Override
                    public void confirmed(Bundle bundle) {
                        ShareUtil.showCustomShareDialog(activity);
                    }
                });

                break;
            case R.id.tip_layout:
                DialogUtil.showRewardDialog(activity, new ConfirmListener() {
                    @Override
                    public void confirmed(Bundle bundle) {
                        homeController.changeToPay(bundle.getString("amount"), "reward");
                    }
                });
                break;
            case R.id.login_ll:
                if (PreferencesUtils.getBoolean(context, "login_success")) {// 己登录点击跳转到用户信息界面

                    UserInfoScene userInfoScene = new UserInfoScene(baseSurfaceView);

                    activity.moveToNewScene(userInfoScene);
                } else {// 没有登录则显示登录对话框
                    DialogUtil.showNativeLoginDialog(activity, new ConfirmListener() {
                        @Override
                        public void confirmed(Bundle bundle) {
                            String type = bundle.getString("type");
                            String phoneStr = bundle.getString("phone");
                            boolean isThirdLogin = bundle.containsKey("login_type");

                            if (TextUtils.equals(type, "regist")) {// 注册
                                String passwordStr = bundle.getString("password");
                                String codeStr = bundle.getString("code");
                                homeController.doRegister(passwordStr, phoneStr, codeStr);
                            } else if (TextUtils.equals(type, "login")) {// 登录
                                String passwordStr = bundle.getString("password");
                                homeController.doLogin(phoneStr, passwordStr);

                            } else if (TextUtils.equals(type, "find")) {// 找回
                                String codeStr = bundle.getString("code");
                                String passwordStr = bundle.getString("password");
                                homeController.doFindPwd(passwordStr, phoneStr, codeStr);

                            } else if (TextUtils.equals(type, "code")) {// 验证码
                                homeController.getPhoneCode(phoneStr);
                            } else if (isThirdLogin) {
                                String loginType = bundle.getString("login_type");
                                homeController.doThirdLogin(loginType);
                            }
                        }
                    });
                }
                break;
            case R.id.logout_tv:
                DialogUtil.showExitLoginDialog(activity, new ConfirmListener() {
                    @Override
                    public void confirmed(Bundle bundle) {
                        homeController.resetUserData();
                        logoutTV.setVisibility(View.GONE);
                        changeLoginState();
                    }
                });
//                activity.moveToNewScene(new SettingScene(baseSurfaceView));
                break;
            case R.id.vip_iv:
                if (!PreferencesUtils.getBoolean(activity, "login_success")) {
                    ToastUtils.show(activity, "请先登录");
                    return;
                }
                DialogUtil.showPayDialog(activity, new ConfirmListener() {
                    @Override
                    public void confirmed(Bundle bundle) {

                        String amountStr = bundle.getString("amount");
                        double amount = 0;
                        if (!TextUtils.isEmpty(amountStr)) {
                            amount = Double.valueOf(amountStr);
                        }
                        PayScene payScene = new PayScene(baseSurfaceView);

                        payScene.setPayType("plan_pay");
                        payScene.setAmount(amount);
                        activity.moveToNewScene(payScene);

                    }
                });
                break;

        }
    }

    @Override
    public void onRestart() {
        changeLoginState();
        activity.showAd();
    }

    public void changeLoginState() {
        if (PreferencesUtils.getBoolean(activity, "login_success", false)) {
            headIV.setVisibility(View.VISIBLE);
            arrowIV.setVisibility(View.VISIBLE);
            logoutTV.setVisibility(View.VISIBLE);

            String name = PreferencesUtils.getString(activity, "name");
            if (!TextUtils.isEmpty(name))
                loginTV.setText(name);
            else {
                loginTV.setText("");
            }

            String url = PreferencesUtils.getString(activity, "head_img");

            if (PreferencesUtils.getBoolean(activity, "paied")) {
                vipIV.setVisibility(View.GONE);
                activity.hideAd();
            } else {
                vipIV.setVisibility(View.VISIBLE);
            }

            if (!TextUtils.isEmpty(url))
                setHeadImg(url);
        } else {
            vipIV.setVisibility(View.GONE);
            headIV.setVisibility(View.GONE);
            arrowIV.setVisibility(View.GONE);
            loginTV.setText("点击登录");
        }
    }

    private void setHeadImg(final String url) {
        activity.executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    final Bitmap bitmap = ImageUtil.getBitmapFromUrl(url);

                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            headIV.setBitmap(bitmap);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void handleRequestSuccess(Bean bean) {
        homeController.handleRequestSuccess(bean);
    }


}
